#!/bin/bash

# Here we figure out DL_SERVER, SOURCE_ROOT, WEB_DIR, etc.
. /vagrant/vagrant/functions.sh

echo_and_log "--> Starting Vagrant provision.sh..."


# All YUM repo work has been moved to pre_provision.sh

# Setting up PHP and Apache server
#./_setup_apache.sh .. vagrant

# Prepare the composer files
if [ -d /vagrant/webapp ]; then
cd /vagrant/vagrant && ./prepare_composer.sh
fi

# Create the necessary tables/users for MySQL.
echo_and_log "--> Creating DEV and TESTING databases for UCX"
sudo $SOURCE_ROOT/vagrant/createdb.sh ucx   ucx    ucx 1>/dev/null
sudo $SOURCE_ROOT/vagrant/createdb.sh ucx_t ucx_t  ucx_t 1>/dev/null
sudo $SOURCE_ROOT/vagrant/createdb.sh cmis cmis cmis 1>/dev/null

# Grant privileages for cmis #18268
mysql <<EOF
GRANT ALL PRIVILEGES ON *.* TO 'cmis'@'%' IDENTIFIED BY 'cmis';
FLUSH PRIVILEGES;
EOF
if [ -d /vagrant/webapp ]; then

echo_and_log "--> Migrating database schema..."
# Run database migrations
 sudo $WEB_DIR/yii migrate --interactive=0 >/dev/null
# sudo $WEB_DIR/tests/codeception/bin/yii migrate --interactive=0
echo_and_log "--> Migrating admin@fp.com as full-function ..."
 sudo $WEB_DIR/yii upgrade-full-function admin@fp.com
# sudo $WEB_DIR/tests/codeception/bin/yii upgrade-full-function admin@fp.com
fi

# Create sym-link to phpmyadmin if needed
#if [ ! -L $WEB_DIR/web/phpmyadmin ]; then
#    sudo ln -sf $WEB_DIR/vendor/fillup/phpmyadmin-minimal $WEB_DIR/formapp/web/phpmyadmin
#fi

#Setup cloud service
#echo_and_log "--> Invoking serviceplayer.sh"
#sudo $SOURCE_ROOT/vagrant/servicelayer.sh

#codecept
#sudo cp /vagrant/webapp/tests/codecept.phar /usr/bin/codecept
#sudo chmod +x /usr/bin/codecept
grep -q 'PATH=$PATH:/vagrant/webapp/vendor/bin' /etc/profile

if [ $? == 1 ]; then
 sed -i '$a export PATH=$PATH:/vagrant/webapp/vendor/bin' /etc/profile
 export PATH=$PATH:/vagrant/webapp/vendor/bin
else
 echo_and_log "--> The vendor/bin has been added into the PATH"
fi

echo_and_log "--> Provision.sh finished in ${SECONDS}s"
